package com.grass.p019lv.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.core.content.ContextCompat;
import java.util.Locale;

/* renamed from: com.grass.lv.view.CustomSeekBar */
public class CustomSeekBar extends View {
    private final String TAG;
    private int cacheProgress;
    private int cacheProgressBarColor;
    private int cacheProgressBarHeight;
    private Paint cacheProgressBarPaint;
    private float dia;
    IProgressListener iProgressListener;
    private boolean mDrag;
    private int maxProgress;
    private int minCircleRadius;
    private int progress;
    private int progressBarColor;
    private int progressBarHeight;
    private Paint progressBarPaint;
    private int seekBarDefaultWidth;
    private int textBgColor;
    private int textBgHeight;
    private Paint textBgPaint;
    private int textBgWidth;
    private int textColor;
    private int textHeight;
    private Paint textPaint;
    private int textSize;

    /* renamed from: com.grass.lv.view.CustomSeekBar$IProgressListener */
    public interface IProgressListener {
        void progress(int i);
    }

    public CustomSeekBar(Context context) {
        this(context, null);
    }

    public CustomSeekBar(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public CustomSeekBar(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.TAG = getClass().getSimpleName();
        this.cacheProgressBarPaint = new Paint();
        this.progressBarPaint = new Paint();
        this.textPaint = new Paint();
        this.textBgPaint = new Paint();
        init();
    }

    private void init() {
        this.seekBarDefaultWidth = getResources().getDisplayMetrics().widthPixels;
        this.textBgWidth = dp2px(200.0f);
        this.textBgHeight = dp2px(16.0f);
        this.cacheProgressBarHeight = dp2px(1.5f);
        this.progressBarHeight = dp2px(1.0f);
        setCircleParam();
        this.textSize = sp2px(10.0f);
        this.progressBarColor = Color.parseColor("#d9ead3");
        this.cacheProgressBarColor = Color.parseColor("#ffffff");
        this.textColor = Color.parseColor("#6e6e6e");
        this.textBgColor = Color.parseColor("#ffffff");
        this.progressBarPaint.setColor(this.progressBarColor);
        this.progressBarPaint.setAntiAlias(false);
        this.progressBarPaint.setStyle(Paint.Style.FILL);
        this.progressBarPaint.setStrokeCap(Paint.Cap.ROUND);
        this.cacheProgressBarPaint.setColor(this.cacheProgressBarColor);
        this.cacheProgressBarPaint.setAntiAlias(false);
        this.cacheProgressBarPaint.setStyle(Paint.Style.FILL);
        this.textBgPaint.setColor(this.textBgColor);
        this.textBgPaint.setAntiAlias(true);
        this.textBgPaint.setStyle(Paint.Style.FILL);
        this.textPaint.setColor(this.textColor);
        this.textPaint.setAntiAlias(true);
        this.textPaint.setStyle(Paint.Style.FILL);
        this.textPaint.setTypeface(Typeface.DEFAULT);
        this.textPaint.setTextSize((float) this.textSize);
        int textHeight2 = getTextHeight(this.textPaint, "00:00/00:00");
        this.textHeight = textHeight2;
        this.textBgHeight = textHeight2 + dp2px(8.0f);
    }

    private void setCircleParam() {
        int i = this.progressBarHeight;
        this.minCircleRadius = i;
        this.dia = (float) (i * 2);
    }

    /* access modifiers changed from: protected */
    public void onMeasure(int i, int i2) {
        int mode = View.MeasureSpec.getMode(i);
        int size = View.MeasureSpec.getSize(i);
        int mode2 = View.MeasureSpec.getMode(i2);
        int size2 = View.MeasureSpec.getSize(i2);
        if (mode != 1073741824) {
            int i3 = this.seekBarDefaultWidth;
            size = mode == Integer.MIN_VALUE ? Math.min(i3, size) : i3;
        }
        if (mode2 != 1073741824) {
            int i4 = this.textBgHeight;
            size2 = mode2 == Integer.MIN_VALUE ? Math.min(i4, size2) : i4;
        }
        setMeasuredDimension(size, size2);
    }

    /* access modifiers changed from: protected */
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getHeight();
        float width = (float) getWidth();
        float f = this.dia;
        float f2 = width - (2.0f * f);
        int i = height >> 1;
        int i2 = this.progressBarHeight;
        canvas.drawRect(f, (float) (i - (i2 >> 1)), width - f, (float) ((i2 >> 1) + i), this.progressBarPaint);
        float f3 = this.dia;
        float f4 = ((((float) this.cacheProgress) * f2) / ((float) this.maxProgress)) + f3;
        int i3 = this.cacheProgressBarHeight;
        canvas.drawRect(f3, (float) (i - (i3 >> 1)), f4, (float) ((i3 >> 1) + i), this.cacheProgressBarPaint);
        if (this.progress < 0) {
            this.progress = 0;
        }
        int i4 = this.progress;
        int i5 = this.maxProgress;
        if (i4 > i5) {
            this.progress = i5;
        }
        String progressText = getProgressText();
        int textWidth = getTextWidth(this.textPaint, progressText);
        int dp2px = dp2px(12.0f) + textWidth;
        this.textBgWidth = dp2px;
        int i6 = this.progress;
        int i7 = this.maxProgress;
        float f5 = (((((float) i6) * f2) / ((float) i7)) - ((((float) dp2px) / ((float) i7)) * ((float) i6))) + this.dia;
        int i8 = this.textBgHeight;
        canvas.drawRoundRect(f5, (float) (i - (i8 >> 1)), f5 + ((float) dp2px), (float) ((i8 >> 1) + i), (float) (dp2px >> 1), (float) (dp2px >> 1), this.textBgPaint);
        canvas.drawText(progressText, f5 + ((float) ((this.textBgWidth >> 1) - (textWidth >> 1))), (float) (i + (this.textHeight >> 1)), this.textPaint);
    }

    private int getTextWidth(Paint paint, String str) {
        Rect rect = new Rect();
        paint.getTextBounds(str, 0, str.length(), rect);
        return rect.width();
    }

    private int getTextHeight(Paint paint, String str) {
        Rect rect = new Rect();
        paint.getTextBounds(str, 0, str.length(), rect);
        return rect.height();
    }

    private String getProgressText() {
        String formatProgress = formatProgress(this.progress);
        String formatProgress2 = formatProgress(this.maxProgress);
        return formatProgress + "/" + formatProgress2;
    }

    private String formatProgress(int i) {
        if (i <= 0) {
            return "00:00";
        }
        if (i < 60) {
            return String.format(Locale.getDefault(), "00:%02d", Integer.valueOf(i % 60));
        } else if (i < 3600) {
            return String.format(Locale.getDefault(), "%02d:%02d", Integer.valueOf(i / 60), Integer.valueOf(i % 60));
        } else {
            return String.format(Locale.getDefault(), "%02d:%02d:%02d", Integer.valueOf(i / 3600), Integer.valueOf((i % 3600) / 60), Integer.valueOf(i % 60));
        }
    }

    public void setMaxProgress(int i) {
        this.maxProgress = i;
    }

    public void cacheProgress(int i) {
        this.cacheProgress = i;
        postInvalidate();
    }

    public void progress(int i) {
        if (!this.mDrag) {
            this.progress = i;
            postInvalidate();
        }
    }

    public void setCacheProgressBarHeight(float f) {
        this.cacheProgressBarHeight = dp2px(f);
    }

    public void setProgressBarHeight(float f) {
        this.progressBarHeight = dp2px(f);
        setCircleParam();
    }

    public void setCacheProgressBarColor(int i) {
        int color = ContextCompat.getColor(getContext(), i);
        this.cacheProgressBarColor = color;
        this.cacheProgressBarPaint.setColor(color);
    }

    public void setProgressBarColor(int i) {
        int color = ContextCompat.getColor(getContext(), i);
        this.progressBarColor = color;
        this.progressBarPaint.setColor(color);
    }

    public void setTextColor(int i) {
        int color = ContextCompat.getColor(getContext(), i);
        this.textColor = color;
        this.textPaint.setColor(color);
    }

    public void setTextBgColor(int i) {
        int color = ContextCompat.getColor(getContext(), i);
        this.textBgColor = color;
        this.textBgPaint.setColor(color);
    }

    public void setTextSize(int i) {
        this.textSize = sp2px((float) i);
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        if (action == 0) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            this.mDrag = true;
        } else if (action == 1) {
            this.mDrag = false;
            touchUpdate(motionEvent.getX());
            IProgressListener iProgressListener2 = this.iProgressListener;
            if (iProgressListener2 != null) {
                iProgressListener2.progress(this.progress);
            }
        } else if (action == 2) {
            this.mDrag = true;
            touchUpdate(motionEvent.getX());
        } else if (action == 3) {
            this.mDrag = false;
            touchUpdate(motionEvent.getX());
            IProgressListener iProgressListener3 = this.iProgressListener;
            if (iProgressListener3 != null) {
                iProgressListener3.progress(this.progress);
            }
        }
        return true;
    }

    private void touchUpdate(float f) {
        this.progress = (int) ((f * ((float) this.maxProgress)) / ((float) getWidth()));
        postInvalidate();
    }

    public void setProgressListener(IProgressListener iProgressListener2) {
        this.iProgressListener = iProgressListener2;
    }

    private int dp2px(float f) {
        return (int) ((f * getContext().getResources().getDisplayMetrics().density) + 0.5f);
    }

    private int sp2px(float f) {
        return (int) ((f * getContext().getResources().getDisplayMetrics().scaledDensity) + 0.5f);
    }
}
